import { FakeApi, random } from '@wowon/utils';
import { getCurrentUser } from '../../auth-center';

// 随机递进生成版本号数据
const getVersion = (() => {
  const typeMap = ['major', 'minor', 'patch'];
  let currentVersion = [1, 0, 0];
  let init = true;

  return () => {
    let type = 0;
    if (!init) {
      let up_sign = false;
      const up_probability = [random(30), random(7), 0];
      currentVersion = currentVersion.map((v, i) => {
        if (up_sign) return 0;
        if (up_probability[i] >= 1) return v;
        up_sign = true;
        type = i;
        return v + 1;
      });
    }
    init = false;
    return [currentVersion.join('.'), typeMap[type]];
  };
})();

const content = `
### 更新内容

- 💄 统一 Modal、Drawer、notification 和 Tour 关闭按钮的样式，并增大 Modal 和 Drawer 关闭按钮的交互区域使其更容易点击。#47909
- 🐞 修复 Badge 和文本元素在 Space 下排版不对齐的问题。#47908
- 💄 修复 Upload 列表行动点换行的问题。#47898
- 🐞 修复 Typography 配置的 ellipsis.symbol 超出单行高度时，省略行数不正确的问题。#47889
- 🐞 修复 DatePicker 配置自定义 controlHeight 时展示高度不正确的问题。#47874
- 🐞 修复 DatePicker 设置 multiple 多选换行时行与行之间没有间隙的问题。#47821
- 💄 调深 Empty 文本色彩使其更清晰可读一些。#47268 @evgmol
`;

const initData = Array.from({ length: random(30, 100) })
  .map(() => {
    const [version, type] = getVersion();
    return { version, type, content };
  })
  .reverse();

export const updateLogList = new FakeApi(initData, {
  queryType: {
    version: 'like',
  },
});

export default {
  'GET /mock/update-log/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await updateLogList.query(req.query);
    res.json(result);
  },
  'POST /mock/update-log/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await updateLogList.create(req.body);
    res.json(result);
  },
  'POST /mock/update-log/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await updateLogList.update(req.body);
    res.json(result);
  },
  'POST /mock/update-log/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await updateLogList.remove(req.body);
    res.json(result);
  },
};
